//
//  uMengAppDelegate.m
//  uMeng
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-6-4.
//  Copyright 2011年 Mtime. All rights reserved.
//

#import "uMengAppDelegate.h"
#import "Reachability.h"
#import "NavigationController.h"
#import "RootViewController.h"
#import "CONSTS.h"

@interface uMengAppDelegate() 

- (void) reachabilityChanged: (NSNotification* )note;

@end

@implementation uMengAppDelegate

@synthesize nav = _nav;

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
    
     RootViewController *rootViewController = [[[RootViewController alloc] init] autorelease];
    
    _nav = [[NavigationController alloc] initWithRootViewController: rootViewController];
   // _nav.view.frame = CGRectMake(0, 20, 320, 460);
    [_window addSubview: _nav.view];
    _window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    //Change the host name here to change the server your monitoring
    _internetReach = [Reachability reachabilityForInternetConnection];
	[_internetReach startNotifier];
	NetworkStatus netStatus = [_internetReach currentReachabilityStatus];
    if (!netStatus) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"网络连接失败" message: nil delegate: nil cancelButtonTitle: @"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
	}

    return YES;
}

- (void)dealloc
{
    [_internetReach release];
    [_nav release];
    [_window release];
    [super dealloc];
}

//监视网络状态,状态变化调用该方法.
- (void) reachabilityChanged: (NSNotification* )note
{
	NetworkStatus netStatus = [_internetReach currentReachabilityStatus];
	if (!netStatus) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"网络连接失败" message: nil delegate: nil cancelButtonTitle: @"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];	
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

- (void)showGlobalIndicator:(id)isShow{
	UIView *indicatorView = [[_window viewWithTag:kTagWindowIndicatorView] retain];
	if (indicatorView == nil) {
		indicatorView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
		indicatorView.backgroundColor = [UIColor redColor];
		indicatorView.tag = kTagWindowIndicatorView;
		[_window addSubview:indicatorView];
	}
	if (isShow) {
		[indicatorView setHidden:NO];
		[_window bringSubviewToFront:indicatorView];
	}else {
		[indicatorView setHidden:YES];
		[_window sendSubviewToBack:indicatorView];
	}
    
	[indicatorView release];
}

@end
